package com.exprecards.common.web;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FileUtils;
import org.richfaces.event.FileUploadEvent;
import org.richfaces.exception.FileUploadException;
import org.richfaces.model.UploadedFile;

import com.exprecards.common.enums.ETipoUsuario;
import com.exprecards.common.persistence.DAOFactory;
import com.exprecards.common.persistence.dto.ExpTbEmpresa;
import com.exprecards.common.persistence.dto.ExpTbEmpresaPk;
import com.exprecards.common.persistence.exceptions.ExpTbEmpresaDaoException;
import com.exprecards.common.persistence.exceptions.ExpTbPgDaoException;
import com.exprecards.common.persistence.exceptions.ExpTbUsuarioDaoException;
import com.exprecards.common.util.WebUtils;

@ManagedBean(name = "crudEmpresaBean")
@ViewScoped
public class CRUDEmpresaBean extends GenericCrudBean<ExpTbEmpresa> {

	private static final long serialVersionUID = 1L;

	
	@ManagedProperty("#{userSessionBean}")
	private UserSessionBean userSessionBean;
	
	
	private int tipoUsuario;
	
	private List<SelectItem> usuariosList = null;
	
	
	private boolean  showPopupPanelDuplicados = true;
	
	
	public boolean isShowPopupPanelDuplicados() {
		return showPopupPanelDuplicados;
	}

	public void setShowPopupPanelDuplicados(boolean showPopupPanelDuplicados) {
		this.showPopupPanelDuplicados = showPopupPanelDuplicados;
	}

	public CRUDEmpresaBean() throws ExpTbEmpresaDaoException, ExpTbUsuarioDaoException, ExpTbPgDaoException{
		super();
		
		FacesContext facesContext = FacesContext.getCurrentInstance();
		HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(false);
		
		userSessionBean = (UserSessionBean)session.getAttribute("userSessionBean");
		
		this.lista = consultar();
		this.listaSimple = consultarSimple();
		
		// listas
		usuariosList = WebUtils
				.createSelectItem(
						DAOFactory
								.getExpTbUsuarioDao()
								.findWherePTipoUsuarioEquals(
										DAOFactory
												.getExpTbPgDao()
												.findWhereCodigoExternoEquals(
														ETipoUsuario.ADMINISTRADOR_EMPRESA
																.getValue())[0]
												.getId()), false);

	}
	
	
	 public void listener(FileUploadEvent event) {
	    	
	    	UploadedFile item = event.getUploadedFile();
	    	
	    	//Escritura Archivo
	        File archivo = new File(CacheParametrosBean.getInstance().getPathContexto() + CacheParametrosBean.getInstance().getPathArchivos() + item.getName());
	        
	        try {
				FileUtils.writeByteArrayToFile(archivo, item.getData());
			} catch (FileUploadException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
	        
	        this.valorActual.setUrlImagen(item.getName()); 
	        
	}
	 
	
	@Override
	protected boolean create() throws Exception{
		
		boolean result = false;
		
		this.valorActual.setEstado("A");
		
		DAOFactory.getExpTbEmpresaDao().insert(this.valorActual);
			
		result=true;
		
		return result;
	}
	
	@Override
	protected boolean update() throws Exception{
		
		boolean result = false;
		
		DAOFactory.getExpTbEmpresaDao().update(new ExpTbEmpresaPk(this.valorActual.getId()),this.valorActual);
		
		return result;
	}

	@Override
	protected void editable(int id) throws Exception{
		valorActual = DAOFactory.getExpTbEmpresaDao().findByPrimaryKey(id);
	}

	@Override
	protected void delete(int id) throws Exception{
		DAOFactory.getExpTbEmpresaDao().delete(new ExpTbEmpresaPk(id));
	}

	@Override
	protected List<Map<String, String>> consultar() {
		
		return null;
	}

	
	@Override
	protected ExpTbEmpresa[] consultarSimple() throws ExpTbEmpresaDaoException {
			return DAOFactory.getExpTbEmpresaDao().findAll();
	}
	
	@Override
	protected ExpTbEmpresa createNew() {
		return new ExpTbEmpresa();
	}

	@Override
	protected boolean isId() {
		boolean result = false;
		if(valorActual.getId()==0){
			result = false;  
		}
		else{
			result = true;
		}
		return result;
	}
	
	@Override
	protected void clean(){
		
	}
	

	/**
	 * @return the userSessionBean
	 */
	public UserSessionBean getUserSessionBean() {
		return userSessionBean;
	}

	/**
	 * @param userSessionBean the userSessionBean to set
	 */
	public void setUserSessionBean(UserSessionBean userSessionBean) {
		this.userSessionBean = userSessionBean;
	}
	

	public int getTipoUsuario() {
		return tipoUsuario;
	}

	public void setTipoUsuario(int tipoUsuario) {
		this.tipoUsuario = tipoUsuario;
	}

	public List<SelectItem> getUsuariosList() {
		return usuariosList;
	}

	public void setUsuariosList(List<SelectItem> usuariosList) {
		this.usuariosList = usuariosList;
	}

}
